import time

from common.utils import BlogDriver
from selenium.webdriver.common.by import By

class BlogList:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = BlogDriver.driver
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver.get(self.url)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 点击登陆
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

#编辑博客内容
    def ListTest(self):
        self.driver.implicitly_wait(5)
        time.sleep(2)
        #点击写博客
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        time.sleep(2)
        #输入博客内容名称
        self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys('博客主页测试')
        #点击提交
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(60) > div.title").text
        BlogDriver.getScreenShot()
        #断言是否发布成功
        #assert text=="博客主页测试"

#修改博客
    def ListEdiTest(self):
        self.driver.implicitly_wait(2)
        #点击查看全文
        # 添加截图
        BlogDriver.getScreenShot()
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(2) > a").click()
        #点击编辑
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)").click()
        time.sleep(2)
        #清除原本博客内容
        self.driver.find_element(By.CSS_SELECTOR,"#title").clear()
        #输入更新博客内容
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys('博客测试')
        #点击提交
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 添加截图
        BlogDriver.getScreenShot()
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(2) > div.title").text
        time.sleep(1)

        assert text=='博客测试'

    def ListDelTest(self):
        self.driver.implicitly_wait(2)
        BlogDriver.getScreenShot()
        #保留原本博客标题
        text1 = self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(2) > div.title").text
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(2) > a").click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        alert=self.driver.switch_to.alert
        alert.accept()
        text2=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(2) > div.title").text
        assert not text1==text2
        BlogDriver.getScreenShot()


